package com.lechi.web.listener;

import com.lechi.web.common.config.LeChiConfig;
import com.lechi.web.common.utils.DateUtils;
import com.lechi.web.framework.log.IotExceptionLog;
import com.lechi.web.service.IIotFtpUploadsService;
import com.lechi.web.system.domain.IotFtpUploads;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import net.contentobjects.jnotify.JNotify;
import net.contentobjects.jnotify.JNotifyAdapter;
import net.contentobjects.jnotify.JNotifyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.Date;
import java.util.concurrent.CompletableFuture;


/**
 * The type Jnotify adapter.
 */
@Slf4j
@Component
public class JnotifyAdapter extends JNotifyAdapter {
    /**
     * 监听程序Id
     */
    public int watchID;
    /**
     * 关注目录的事件
     */
    int mask = JNotify.FILE_CREATED | JNotify.FILE_DELETED;
    /**
     * 是否监视子目录，即级联监视
     */
    boolean watchSubtree = true;
    @Getter
    @Value("${ftp.watch.path}")
    private String uploadPath;
    @Getter
    @Value("${ftp.watch.timeout}")
    private Long timeout;
    @Autowired
    private IIotFtpUploadsService iotFtpUploadsService;

    @Autowired
    private LeChiConfig leChiConfig;

    /**
     * 容器启动时启动监视程序
     */
    public void beginWatch() {
        try {
            this.watchID = JNotify.addWatch(uploadPath, mask, watchSubtree, this);
            log.error("jnotify -----------启动成功2-----------");
        } catch (JNotifyException e) {
            log.error("JnotifyAdapter beginWatch", e);
        }
        /**
         * 死循环，线程一直执行，休眠一分钟后继续执行，主要是为了让主线程一直执行 休眠时间和监测文件发生的效率无
         * （就是说不是监视目录文件改变一分钟后才监测到，监测几乎是实时的，调用本地系统库）
         */
        while (true) {
            try {
                Thread.sleep(timeout);
            } catch (InterruptedException e) {// ignore it
            }
        }
    }

    /**
     * 文件创建
     *
     * @param wd       监听程序Id 初始为1，多个监控程序以此加1
     * @param rootPath 目录名
     * @param name     文件名
     */

    @Override
    public void fileCreated(int wd, String rootPath, String name) {
        log.error(wd + "----->文件被创建, 创建位置为： " + rootPath + File.separator+ name);
        IotFtpUploads iotFtpUploads = new IotFtpUploads();
        try {
            String separator = File.separator;
            String[] stirs = name.split(separator);
            iotFtpUploads.setDeviceStation(stirs[0]);
            iotFtpUploads.setFilepath(rootPath + name);
            iotFtpUploads.setFilename(name);
            // 图片文件带有时间戳
            String dateTime = name.substring(name.lastIndexOf(stirs[1]), name.lastIndexOf("."));
            Date createTime = DateUtils.parseDate(dateTime, DateUtils.YYYYMMDDHHMMSS);
            iotFtpUploads.setCreateTime(createTime);
            iotFtpUploadsService.insertIotFtpUploads(iotFtpUploads);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "Ftp文件notify监听服务异常", method, iotFtpUploads));
        }
    }
}
